import tkinter as tk;
import tkinter.messagebox;
from tkinter import ttk;
import threading;
from time import sleep as timesleep;
import pyautogui;
from sys import exit as sysexit;
from plyer import notification;
from webbrowser import open_new;
from ctypes import windll as ctypeswindll;
from keyboard import is_pressed as keyboardis_pressed;
stop = False;
window_top = False;
program_closes = True;
no_notice = False;
pyautogui.PAUSE = 0;
if not ctypeswindll.shell32.IsUserAnAdmin():
    tkinter.messagebox.showerror("错误","请以管理员权限运行程序，因为需要侦测键盘");
    sysexit();
value = 0;
def str_historical_updates():
    temp = "";
    global historical_updates;
    #将字典historical_updates全部转化为字符串
    for key,value in historical_updates.items():
        temp = temp + key + "\n" + value + "\n";#用temp变量将返回的结果装下来
    return temp;
def val(number):
    global value;
    value = number;
def thread_wait_press():
    global stop;
    while keyboardis_pressed('ctrl+t') == False:
        pass;timesleep(0.01);
    stop = True;
def thread_click_number():
    global stop,click_number;
    while stop != True:
        timesleep(1.01);
        if stop != True:#这里是防止用户在等待的时间就停止
            if combo_selected.get() == "长按左键" or combo_selected.get() == "长按右键":
                lb_click_count.config(bg="green",text="每秒点击次数：当前是长按");
            else:
                lb_click_count.config(bg="green",text="每秒点击次数："+str(click_number));
        click_number = 0;
def func_detect_presses():
    global sleep_time,entered,stop,click_number,combo_selected;
    while program_closes:
        while keyboardis_pressed('ctrl+k')==False:pass;timesleep(0.01);
        stop = False;
        try:
            sleep_time=float(entered.get());
        except:
            tkinter.messagebox.showerror("错误","你输入的数值不符合要求，请重新输入");
            continue;
        if not sleep_time <= 1.0 or not sleep_time >= 0.0:
            tkinter.messagebox.showerror("错误","你输入的数值不符合要求，请重新输入");
        elif combo_selected.get() == "":
            tkinter.messagebox.showerror("错误","你没有选择点击的键");
        else:
            if not no_notice:
                notification.notify(title = "连点器",message = "即将开始点击",app_icon = None,timeout = 10)
            lb_click_count.config(bg="green",text="每秒点击次数：即将开始点击")
            thread_wait = threading.Thread(target=thread_wait_press);
            thread_click_number_1 = threading.Thread(target=thread_click_number);
            click_number = 0;
            thread_wait.start();
            thread_click_number_1.start();
            while not stop:
                if combo_selected.get() == "单击左键":
                    pyautogui.click(button="left");
                    timesleep(sleep_time);
                if combo_selected.get() == "单击右键":                                                                                  
                    pyautogui.click(button="right");
                    timesleep(sleep_time);
                if combo_selected.get() == "双击左键":                                                                                  
                    pyautogui.doubleClick(button="left");
                    timesleep(sleep_time);
                if combo_selected.get() == "双击右键":                                                                                  
                    pyautogui.doubleClick(button="right");
                    timesleep(sleep_time);
                if combo_selected.get() == "三击左键":                                                                                  
                    pyautogui.tripleClick(button="left");
                    timesleep(sleep_time);
                if combo_selected.get() == "三击右键":                                                                                  
                    pyautogui.tripleClick(button="right");
                    timesleep(sleep_time);
                if combo_selected.get() == "长按左键":                                                                                  
                    pyautogui.mouseDown(button="left")
                    while not stop:timesleep(0.01);
                    pyautogui.mouseUp(button = 'left');
                if combo_selected.get() == "长按右键":                                                                                  
                    pyautogui.mouseDown(button="right");
                    while not stop:timesleep(0.01);
                    pyautogui.mouseUp(button = 'right');
                click_number = click_number + 1;
            lb_click_count.config(bg="grey",text="每秒点击次数：当前未连点");
            if not no_notice:
                notification.notify(title = "连点器",message = "已结束",app_icon = None,timeout = 10);
    sysexit();
def func_top_floor():
    global window,window_top;
    if window_top != True:
        window.attributes('-topmost', 'true');
        window_top = True
    else:
        window.attributes('-topmost', 'false');
        window_top = False
def func_about_exit():
    about_window.destroy();
    window.attributes('-disabled',False);
def window_undisable():
    window.attributes('-disabled',False);
    about_window.destroy();
def individual():
    open_new('https://space.bilibili.com/3493083240991380')
def func_about():
    global about_window,window;
    window.attributes('-topmost', 'false');
    window.attributes('-disabled',True);
    about_window = tk.Tk();
    about_window.title("关于连点器");
    about_window.geometry("150x180");
    about_window.resizable(False,False);
    about_window.attributes("-toolwindow", 2);
    lb_about = tk.Label(about_window,text="鼠标连点器  V2.0.2\n-2023年12月30日-\n本次更新内容：\n1、适配sclqx.top\n-liuqx1234-",justify='left');
    bt_about_update = tk.Button(about_window,text="历史更新记录",command=func_history);
    bt_about_exit = tk.Button(about_window,text="关闭",command=func_about_exit);
    bt_individual = tk.Button(about_window,text="   B站个人主页   ",command=individual)
    lb_about.grid(row=0,column=0,padx=(0,0),pady=0,columnspan=2);
    bt_about_exit.grid(row=1,column=1,padx=(0,0),pady=0);
    bt_about_update.grid(row=1,column=0,padx=(0,0),pady=0);
    bt_individual.grid(row=2,column=0,padx=(0,0),pady=2)
    about_window.protocol("WM_DELETE_WINDOW",window_undisable)
    about_window.mainloop();
def func_history(): 
    open_new("https://sclqx.top/liandianqi/history_update.html")
def window_exit():
    global program_closes;
    window.destroy();
    program_closes = False;
    sysexit(0);
def func_notice_on_off():
    global no_notice;
    if check_no_notice_value.get() == "禁用":
        no_notice = True;
    else:
        no_notice = False;
detect_presses = threading.Thread(target=func_detect_presses);
detect_presses.start();#用多线程判断用户是否按下ctrl+k
window = tk.Tk();
window.title("连点器");
window.geometry("270x200");
window.resizable(False,False);
window.protocol("WM_DELETE_WINDOW",window_exit);
top_floor = False;
top = tk.Menu(window);
top.add_cascade(label="关于",command=func_about);
window.config(menu=top);
lb = tk.Label(window,text="点击间隔(0-1之间)：",width=20,fg="black");
combo_selected = tkinter.StringVar();
combo = ttk.Combobox(window, textvariable=combo_selected,width=7);
combo.configure(state="readonly");
combo["value"] = ("单击左键","单击右键","双击左键","双击右键","三击左键","三击右键","长按左键","长按右键");
entered = tkinter.StringVar();
text = tk.Entry(window,width=4,textvariable=entered);
lb_2 = tk.Label(window,text="选择点击类型：",width=24,fg="black",height=2);
lb_3 = tk.Label(window,text="按下 contral+k 开始   按下 contral+t 停止",width=34,fg="black");
lb_click_count = tk.Label(window,text="每秒点击次数：当前未连点",bg="grey",font=("微软雅黑", 10));
check_top_set_value = tk.StringVar();
check_top_window=ttk.Checkbutton(window,text="置顶窗口",variable=check_top_set_value,onvalue="置顶",offvalue="不置顶",command=func_top_floor);
lb_prompt = tk.Label(window,text="提示：如果是长按，请忽略点击间隔输入框；如\n果选择的是双击/三击，每秒点击次数就指的是双\n击/三击了多少下；CPU越好，点击次数越多");
check_no_notice_value = tk.StringVar();
check_no_notice = ttk.Checkbutton(window,text="禁用通知",variable=check_no_notice_value,onvalue="禁用",offvalue="不禁用",command=func_notice_on_off);
lb.grid(row=0,column=0,padx=(0,0),pady=0,sticky="W");
text.grid(row=0,column=1,padx=(0,0),pady=0,sticky="W");
combo.grid(row=1,column=1,padx=(0,0),pady=0,sticky="W");
lb_2.grid(row=1,column=0,padx=(0,0),pady=0,sticky="W");
lb_3.grid(row=2,column=0,padx=(0,0),pady=0,columnspan=2);
check_top_window.grid(row=3,column=0,padx=(0,0),pady=0);
check_no_notice.grid(row=3,column=1,padx=(0,0),pady=0)
lb_click_count.grid(row=4,column=0,padx=(0,0),columnspan=2);
lb_prompt.grid(row=5,column=0,padx=(0,0),columnspan=2);
tk.mainloop();